//
//  YXTableSectionHeaderView.m
//  MockProject
//
//  Created by FanYu on 23/8/2016.
//  Copyright © 2016 niuzhaowang. All rights reserved.
//

#import "YXTableSectionHeaderView.h"


@interface YXTableSectionHeaderView ()

@property (nonatomic, strong) UIView *hairlineView;

@end


@implementation YXTableSectionHeaderView

- (instancetype)initWithReuseIdentifier:(nullable NSString *)reuseIdentifier {
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

#pragma mark - Setup UI
- (void)setupUI {
    // self
    self.contentView.backgroundColor = [UIColor colorWithHexString:@"f5f5f5"];
    
    // hairline
    self.hairlineView = [[UIView alloc] init];
    self.hairlineView.backgroundColor = [UIColor colorWithHexString:@"e0dcd9"];
    [self addSubview:self.hairlineView];
    
    [self.hairlineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(self.mas_bottom);
        make.height.mas_equalTo(0.5);
    }];
    
    // action button
    self.actionButton = [[UIButton alloc] init];
    [self.actionButton setTitleColor:[UIColor colorWithHexString:@"403e3d"] forState:UIControlStateNormal];
    [self.actionButton setTitleColor:[UIColor colorWithHexString:@"ff7000"] forState:UIControlStateHighlighted];
    self.actionButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.actionButton.imageView setContentMode:UIViewContentModeScaleAspectFit];
    [self addSubview:self.actionButton];
    
    [self.actionButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.actionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    // info label
    self.infoLable = [[UILabel alloc] init];
    self.infoLable.font = [UIFont systemFontOfSize:13];
    self.infoLable.textColor = [UIColor colorWithHexString:@"a5a2a1"];
    self.infoLable.textAlignment = NSTextAlignmentRight;
    [self addSubview:self.infoLable];
    
    [self.infoLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-15);
        make.centerY.equalTo(self.mas_centerY);
    }];
}

#pragma mark - Actions 
- (void)buttonTapped:(UIButton *)sender {
    [self.delegate headerButtonTapped:sender];
    
    // change title
    if ([sender.titleLabel.text isEqualToString:@" 删除管理"]) {
        [sender setTitle:@"取消" forState:UIControlStateNormal];
        [sender setTitleColor:[UIColor colorWithHexString:@"ff4600"] forState:UIControlStateNormal];
        [sender setImage:nil forState:UIControlStateNormal];
    } else if ([sender.titleLabel.text isEqualToString:@"取消"]) {
        [sender setTitle:@" 删除管理" forState:UIControlStateNormal];
        [sender setTitleColor:[UIColor colorWithHexString:@"403e3d"] forState:UIControlStateNormal];
        [sender setImage:[UIImage imageNamed:@"删除管理按钮"] forState:UIControlStateNormal];
    }
}


#pragma mark - Helper
- (void)setSectionHeaderType:(SectionHeaderType)type isDownloadingAll:(BOOL)isDownloadingAll {
    
    switch (type) {
        case YXDownloadingSectionHeader:
            self.actionButton.tag = 0;
            if (isDownloadingAll) {
                [self.actionButton setTitle:@" 全部暂停" forState:UIControlStateNormal];
                [self.actionButton setImage:[UIImage imageNamed:@"全部暂停按钮"] forState:UIControlStateNormal];
            } else {
                [self.actionButton setTitle:@" 全部开始" forState:UIControlStateNormal];
                [self.actionButton setImage:[UIImage imageNamed:@"全部开始按钮"] forState:UIControlStateNormal];
            }
            break;
            
        case YXDownloadedSectionHeader:
            self.actionButton.tag = 1;
            [self.actionButton setTitle:@" 删除管理" forState:UIControlStateNormal];
            [self.actionButton setImage:[UIImage imageNamed:@"删除管理按钮"] forState:UIControlStateNormal];
            break;
            
        default:
            break;
    }
}


#pragma mark - Public Method
- (void)updateTaskAmount:(SectionHeaderType)type taskAmount:(NSInteger)amount {
    
    switch (type) {
        case YXDownloadingSectionHeader:
            self.infoLable.text = [NSString stringWithFormat:@"正在下载(%ld)",(long)amount];
            break;
        case YXDownloadedSectionHeader:
            self.infoLable.text = [NSString stringWithFormat:@"已经下载(%ld)",(long)amount];
            break;
        default:
            break;
    }
}

- (void)updateDownloadingButton {
    [self.actionButton setTitle:@" 删除管理" forState:UIControlStateNormal];
    [self.actionButton setTitleColor:[UIColor colorWithHexString:@"403e3d"] forState:UIControlStateNormal];
    [self.actionButton setImage:[UIImage imageNamed:@"删除管理按钮"] forState:UIControlStateNormal];
}

@end
